package com.cz.fra.android.logic;

import java.util.ArrayList;
import java.util.List;

import com.cz.fra.android.data.FishReport.BaitTable;
import com.cz.fra.android.entity.Bait;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

public class BaitLogic {

	private static BaitLogic instance;
	
	private ContentResolver cr;

	private Context mContext;
	
	private BaitLogic(Context context) {
		mContext = context;
		cr = mContext.getContentResolver();
	}
	
	public static BaitLogic getInstance(Context context) {
		if (instance == null) {
			instance = new BaitLogic(context);
		}
		return instance;
	}
	
	public synchronized void insertLocations(ArrayList<Bait> list) {
		if (cr == null) {
			cr = mContext.getContentResolver();
		}

		int length = list.size();
		Log.e("insertBaitList start", System.currentTimeMillis() + "");
		ContentValues[] values = new ContentValues[length];
		for (int i = 0; i < length; i++) {
			Bait b = list.get(i);
			values[i] = b.instanceToCV();
		}
		cr.bulkInsert(BaitTable.CONTENT_URI, values);
		synchronized (list) {
			list.clear();
		}
		Log.e("insertBaits end", System.currentTimeMillis() + "");
	}
	
	public List<Bait> getBaits(){
		if (cr == null) {
			cr = mContext.getContentResolver();
		}
		ArrayList<Bait> arrayList = new ArrayList<Bait>();
		Cursor cursor = cr.query(BaitTable.CONTENT_URI, null, null, null, null);
		if (cursor.moveToFirst()) {
			do {
				Bait b = new Bait();
				b.setBait(cursor.getString(cursor.getColumnIndex(BaitTable.BAIT)));
				b.setDescription(cursor.getString(cursor.getColumnIndex(BaitTable.DESCRIPTION)));
				arrayList.add(b);
			}while (cursor.moveToNext());
		}
		cursor.close();
		return arrayList;
		
	}
}
